//
//  Hot_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/8/31.
//  Copyright © 2016年 王道林. All rights reserved.
//
#import "DLMacro.h"
#import "DLWNetworkingParser.h"
#import "Hot_ViewController.h"
#import "BaseCollectionView.h"
#import "BaseView.h"
#import "BaseButton.h"
#import "Masonry.h"
#import "Hot_collectionViewCell.h"
#import "Home_NewsModel.h"
#import "Home_NewsViewControllerWeb.h"
@interface Hot_ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) BaseView *backView;
@property (nonatomic, strong) BaseButton *buttonTop;
@property (nonatomic, strong) BaseButton *buttonDay;
@property (nonatomic, strong) BaseButton *buttonTrends;
@property (nonatomic, strong) UIView *viewStrip;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation Hot_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleAttributes];
    [self handleData];
    [self createSubView];
}

- (void)handleAttributes
{
    self.navigationItem.title = @"热点资讯";
}

#pragma mark -  获取数据
- (void)handleData
{
    // 初始化数组
    _dataArr = [NSMutableArray array];
    [DLWNetworkingParser GETWithURL:@"http://www.danjiguanjia.com/api/News/getHeadlines?page=1" par:nil success:^(id responseObject) {
        // 第一组数据
        NSMutableArray *arrOne = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
            Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
            [arrOne addObject:model];
        }
        [_dataArr addObject:arrOne];
        // 第二组数据
        [DLWNetworkingParser GETWithURL:@"http://www.danjiguanjia.com/api/News/getDailymicro?page=1" par:nil success:^(id responseObject) {
            NSMutableArray *arrTwo = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
                Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
                [arrTwo addObject:model];
            }
            [_dataArr addObject:arrTwo];
            // 第三组数据
            [DLWNetworkingParser GETWithURL:@"http://www.danjiguanjia.com/api/News/getDisease?page=1" par:nil success:^(id responseObject) {
                NSMutableArray *arrThree = [NSMutableArray array];
                for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
                    Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
                    [arrThree addObject:model];
                }
                [_dataArr addObject:arrThree];
                [_collectionView reloadData];
            } filed:^(NSError *error) {
            }];
        } filed:^(NSError *error) {
        }];
    } filed:^(NSError *error) {
    }];
}


#pragma mark - 创建子控件及collectionView
- (void)createSubView
{
    //view
    _backView = [[BaseView alloc] init];
    [self.view addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
        make.height.mas_equalTo(50);
    }];
    // 三个button
    _buttonTop = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonTop.titleLabel.textAlignment = 1;
    [_buttonTop setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [_buttonTop addTarget:self action:@selector(buttonTopAction:) forControlEvents:UIControlEventTouchUpInside];
    [_buttonTop setTitle:@"新闻头条" forState:UIControlStateNormal];
    [_backView addSubview:_buttonTop];
    [_buttonTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left);
        make.width.mas_equalTo(_backView.mas_width).multipliedBy(0.333333333);
        make.top.mas_equalTo(_backView.mas_top);
        make.bottom.mas_equalTo(_backView.mas_bottom).mas_offset(-2);
    }];
    
    _buttonDay = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonDay.titleLabel.textAlignment = 1;
    [_buttonDay setTintColor:[UIColor grayColor]];
    [_buttonDay addTarget:self action:@selector(buttonDayAction:) forControlEvents:UIControlEventTouchUpInside];
    [_buttonDay setTitle:@"微日报" forState:UIControlStateNormal];
    [_backView addSubview:_buttonDay];
    [_buttonDay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_buttonTop.mas_right);
        make.width.mas_equalTo(_backView.mas_width).multipliedBy(0.333333333);
        make.top.mas_equalTo(_backView.mas_top);
        make.bottom.mas_equalTo(_backView.mas_bottom).mas_offset(-2);
    }];
    
    _buttonTrends = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonTrends setTitle:@"疾病动态" forState:UIControlStateNormal];
    [_buttonTrends addTarget:self action:@selector(buttonTrendsAction:) forControlEvents:UIControlEventTouchUpInside];
    _buttonTrends.titleLabel.textAlignment = 1;
    [_buttonTrends setTintColor:[UIColor grayColor]];
    [_backView addSubview:_buttonTrends];
    [_buttonTrends mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_buttonDay.mas_right);
        make.width.mas_equalTo(_backView.mas_width).multipliedBy(0.333333333);
        make.top.mas_equalTo(_backView.mas_top);
        make.bottom.mas_equalTo(_backView.mas_bottom).mas_offset(-2);
    }];
    // 滑条
    _viewStrip = [[UIView alloc] init];
    _viewStrip.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    [_backView addSubview:_viewStrip];
    [_viewStrip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left);
        make.width.mas_equalTo(_backView.mas_width).multipliedBy(0.333333333);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(_backView.mas_bottom);
    }];
    
    // 创建collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 50- 64 - 49);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50 - 64- 49) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
//    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    [self.view addSubview:_collectionView];
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 注册cell
    [_collectionView registerClass:[Hot_collectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

#pragma mark -collection的协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Hot_collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.arrData = _dataArr[indexPath.row];
    cell.cellNumber = indexPath.row;
    cell.blockModel = ^(Home_NewsModel *model)
    {
        Home_NewsViewControllerWeb *NewsVC = [[Home_NewsViewControllerWeb alloc] init];
        NewsVC.newsId = model.cms_id;
        NewsVC.navigationTitle = @"资讯详情";
        [self.navigationController pushViewController:NewsVC animated:YES];
    };
    return cell;
}

#pragma mark - 滑动时候变化
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //  滑条变化
   NSInteger current = scrollView.contentOffset.x / VIEW_WIDTH;
    [UIView animateWithDuration:0 animations:^{
        _viewStrip.frame = CGRectMake(VIEW_WIDTH / 3 * current, 48, VIEW_WIDTH / 3, 2);
    }];
    // button变化
    if (current == 0) {
        [_buttonTop setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
        [_buttonDay setTintColor:[UIColor grayColor]];
        [_buttonTrends setTintColor:[UIColor grayColor]];
    } else if (current == 1)
    {
        [_buttonDay setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
        [_buttonTop setTintColor:[UIColor grayColor]];
        [_buttonTrends setTintColor:[UIColor grayColor]];
    } else
    {
        [_buttonTrends setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
        [_buttonDay setTintColor:[UIColor grayColor]];
        [_buttonTop setTintColor:[UIColor grayColor]];
    }
}

#pragma mark - button的点击方法
// 1
- (void)buttonTopAction:(BaseButton *)button
{
    _collectionView.contentOffset = CGPointMake(0, 0);
    _viewStrip.frame = CGRectMake(0, 48, VIEW_WIDTH / 3, 2);
    [_buttonTop setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [_buttonDay setTintColor:[UIColor grayColor]];
    [_buttonTrends setTintColor:[UIColor grayColor]];
}

- (void)buttonDayAction:(BaseButton *)button
{
    _collectionView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
     _viewStrip.frame = CGRectMake(VIEW_WIDTH / 3, 48, VIEW_WIDTH / 3, 2);
    [_buttonDay setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [_buttonTop setTintColor:[UIColor grayColor]];
    [_buttonTrends setTintColor:[UIColor grayColor]];
}
- (void)buttonTrendsAction:(BaseButton *)button
{
    _collectionView.contentOffset = CGPointMake(VIEW_WIDTH * 2, 0);
     _viewStrip.frame = CGRectMake(VIEW_WIDTH / 3 * 2, 48, VIEW_WIDTH / 3, 2);
    [_buttonTrends setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [_buttonDay setTintColor:[UIColor grayColor]];
    [_buttonTop setTintColor:[UIColor grayColor]];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
